﻿/*
 * Created by SharpDevelop.
 * User: Erik
 * Date: 16.02.2013
 * Time: 20:13
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
using System.Drawing;


namespace MSK
{
    /// <summary>
    /// Class with program entry point.
    /// </summary>
    internal sealed class Program
    {
        /// <summary>
        /// Program entry point.
        /// </summary>
        [STAThread]
        private static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            MainForm mainForm = new MainForm();
            
            List<String> rawLines = new List<String>();
            foreach (var line in File.ReadLines(@"..\..\0DM1082Z.MSK") )
                rawLines.Add(line);
            
            String headerLine = "";
            foreach (var line in rawLines) {
                if (line.StartsWith("*234567890-")) {
                    headerLine = line;
                    break;
                }
            }
            
            String[] fieldReps = headerLine.Split('-');
            int[] fieldLengths = new int[fieldReps.Length];
            for (int i = 0; i < fieldReps.Length; ++i) 
                fieldLengths[i] = fieldReps[i].Length;
            
                
            int cw = 8;
            int ch = 23;
            foreach (var line in rawLines) {
                if (String.IsNullOrEmpty(line) || (line[0] == '*') || (line[0] == ';'))
                    continue;
                
                int s = 0;
                int i = 0;
                int labelLen = fieldLengths[i];
                String fieldName = line.Substring(s                         , fieldLengths[i]);
                String len       = line.Substring(s += fieldLengths[i++] + 1, fieldLengths[i]);
                String yk        = line.Substring(s += fieldLengths[i++] + 1, fieldLengths[i]);
                String xk        = line.Substring(s += fieldLengths[i++] + 1, fieldLengths[i]);
                String type      = line.Substring(s += fieldLengths[i++] + 1, fieldLengths[i]);
                String dec       = line.Substring(s += fieldLengths[i++] + 1, fieldLengths[i]);
                String page      = line.Substring(s += fieldLengths[i++] + 1, fieldLengths[i]);
                String caption   = line.Substring(s += fieldLengths[i++] + 1, fieldLengths[i]);
                while (i < 21)
                    s += fieldLengths[i++] + 1;
                String language   = line.Substring(s += fieldLengths[i++] + 1, fieldLengths[i]);
                
                if (language != "GB")
                    continue;
                
                Label label = new Label();
                label.Location = new Point(Int32.Parse(xk)*cw, Int32.Parse(yk)*ch);
                label.Text = caption.Trim();
                label.Size = new Size(label.Text.Length*cw, ch);
                
                TextBox textBox = new TextBox();
                textBox.Location = new Point((Int32.Parse(xk)+label.Text.Length)*cw, Int32.Parse(yk)*ch);
                textBox.Text = fieldName;
                textBox.Size = new Size(Int32.Parse(len)*cw, ch);

                mainForm.groupBox1.Controls.Add(label);
                mainForm.groupBox1.Controls.Add(textBox);
            }
            
            
            
            
            Application.Run(mainForm);
        }
        
    }
}
